package com.iyitong.common.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.iyitong.common.dao.BaseDao;
import com.iyitong.common.entity.BaseEntity;

public class DaoTemplate implements BaseDao {

	protected SqlMapClient sqlMapClient;
	
	private static final String GET_LAST_ID = "common.selectLastId";
	
	@SuppressWarnings("unchecked")
	@Override
	public List<BaseEntity> queryForList(String sqlMapId, BaseEntity obj) throws SQLException {
		// TODO Auto-generated method stub
		return (List<BaseEntity>)sqlMapClient.queryForList(sqlMapId, obj);
	}

	@Override
	public BaseEntity queryForObject(String sqlMapId, BaseEntity obj) throws SQLException {
		// TODO Auto-generated method stub
		return (BaseEntity)sqlMapClient.queryForObject(sqlMapId, obj);
	}

	@Override
	@Transactional
	public int insert(String sqlMapId, BaseEntity obj) throws SQLException {
		// TODO Auto-generated method stub
		sqlMapClient.insert(sqlMapId, obj);
		return (Integer)sqlMapClient.queryForObject(GET_LAST_ID);
	}

	@Override
	public int update(String sqlMapId, BaseEntity obj) throws SQLException {
		// TODO Auto-generated method stub
		return sqlMapClient.update(sqlMapId, obj);
	}

	@Override
	public int delete(String sqlMapId, BaseEntity obj) throws SQLException {
		// TODO Auto-generated method stub
		return sqlMapClient.delete(sqlMapId, obj);
	}

	public void setSqlMapClient(SqlMapClient sqlMapClient) {
		this.sqlMapClient = sqlMapClient;
	}
}
